<template>
    <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="消息" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="唯一标识" prop="code">
                <el-input
                    v-model="queryParams.code"
                    placeholder="请输入唯一标识"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                >删除
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-row v-show="bizScreenList.length>0" v-loading="loading" :gutter="20" style="margin-top: 10px;font-size: 15px" type="flex" justify="space-around"
                v-for="(item,index) in bizScreenList" :key="index">
            <el-col :xl="24">
                <el-card shadow="always">
                    <div slot="header" class="clearfix">
                        <span style="font-size: 14px;color: #abb2bf">{{ item.name }}</span>
                        <el-button
                            style="float: right; padding: 3px 0"
                            type="text"
                            @click="handleUpdate(item)"
                            v-hasPermi="['business:gateway:edit']"
                        >修改
                        </el-button>
                    </div>
                    <div style="display: flex;align-items: center;color: #717783;margin-top: 10px">
                        <span>唯一标识：</span>
                        <span>{{ item.code }}</span>
                    </div>

                    <div style="display: flex;align-items: center;color: #717783;margin-top: 10px">
                        <span>状态：</span>
                        <el-tag :type="item.isOnline==='1'?'success':'info'">
                            {{ item.isOnline === '1' ? '在线' : '离线' }}
                        </el-tag>
                    </div>


                    <div style="margin-top: 10px">
                        <el-button
                            size="mini"
                            @click="sensorShow(item)"
                        >信息点展示
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(item)"
                        >删除
                        </el-button>
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <el-row v-show="bizScreenList.length<1" v-loading="loading" :gutter="20" style="margin-top: 10px;" type="flex" justify="space-around">
            <el-col :xl="24">
                <el-empty></el-empty>
            </el-col>
        </el-row>


        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改屏幕对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="名称" prop="name" label-width="100px">
                    <el-input v-model="form.name" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="唯一标识" prop="code" label-width="100px">
                    <el-input v-model="form.code" placeholder="请输入唯一标识"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 添加或修改控制点分组对话框 -->
        <el-dialog title="屏幕展示信息点" :visible.sync="screenSensorOpen" width="1000px" append-to-body>
            <el-button @click="addSensor">添加信息点</el-button>
            <el-table :data="screenSensorList" :height="250">

                <el-table-column label="关联信息点" align="center" prop="relationSensor">
                    <template slot-scope="scope">
                        <el-checkbox v-model="scope.row.relationSensor">是</el-checkbox>
                    </template>
                </el-table-column>

                <el-table-column label="网关" align="center" prop="gatewayId">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.gatewayId"
                                   placeholder="选择网关"
                                   v-if="scope.row.relationSensor"
                        >
                            <el-option
                                v-for="item in gatewayList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                                @click.native="tableChangeGateway(item.value, scope.$index)"
                            />
                        </el-select>
                    </template>
                </el-table-column>

                <el-table-column label="信息点" align="center" prop="sensorId">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.sensorId"
                                   placeholder="选择信息点"
                                   v-if="scope.row.relationSensor"
                        >
                            <el-option
                                v-for="item in scope.row.sensorSelect"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </template>
                </el-table-column>

                <el-table-column label="信息点别名" align="center" prop="sensorAlias">
                    <template slot-scope="scope">
                        <el-input v-if="scope.row.relationSensor" v-model="scope.row.sensorAlias"
                                  placeholder="请输入信息点别名"
                        ></el-input>
                    </template>
                </el-table-column>

                <el-table-column label="自定义消息" align="center" prop="customMessage">
                    <template slot-scope="scope">
                        <el-input v-if="!scope.row.relationSensor" v-model="scope.row.customMessage"
                                  placeholder="请输入自定义消息"
                        ></el-input>
                    </template>
                </el-table-column>

                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-link style="margin-left: 5px" type="danger" :underline="false"
                                 @click="deleteSensor(scope.row, scope.$index)"
                        >删除
                        </el-link>
                    </template>
                </el-table-column>

            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitScreenSensor">确 定</el-button>
                <el-button @click="cancelScreenSensor">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listBizScreen,
    getBizScreen,
    delBizScreen,
    addBizScreen,
    updateBizScreen,
    addScreenSensor,
    getScreenSensor
} from '@/api/equipment/bizScreen'
import { listSelectByGatewayId } from '@/api/equipment/sensor'
import { listSelect } from '@/api/equipment/gateway'

export default {
    name: 'BizScreen',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 屏幕表格数据
            bizScreenList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                code: null,
                status: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: '名称不能为空', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '标识不能为空', trigger: 'blur' }
                ]
            },
            gatewayList: [],
            screenSensorList: [],
            screenSensorOpen: false
        }
    },
    created() {
        this.getList()
        this.getGatewayList()
    },
    methods: {

        getList() {
            this.loading = true
            listBizScreen(this.queryParams).then(response => {
                this.bizScreenList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                name: null,
                code: null,
                status: '0',
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加屏幕'
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getBizScreen(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改屏幕'
            })
        },
        sensorShow(row) {
            this.form.id = row.id
            getScreenSensor(row.id).then(response => {
                this.screenSensorList = response.data
                this.screenSensorOpen = true
            })
        },
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBizScreen(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addBizScreen(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除屏幕编号为"' + ids + '"的数据项？').then(function() {
                return delBizScreen(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('stock/bizScreen/export', {
                ...this.queryParams
            }, `bizScreen_${new Date().getTime()}.xlsx`)
        },
        addSensor() {
            let obj = {
                relationSensor: true,
                screenId: this.form.id,
                gatewayId: null,
                sensorId: null,
                sensorAlias: '',
                customMessage: '',
                sensorSelect: []
            }
            this.screenSensorList.push(obj)
        },
        tableChangeGateway(gatewayId, index) {
            this.screenSensorList[index]['sensorId'] = null
            listSelectByGatewayId(gatewayId).then(response => {
                this.screenSensorList[index]['sensorSelect'] = response.data
            })
        },
        deleteSensor(row, index) {
            this.screenSensorList.splice(index, 1)
        },
        getGatewayList() {
            listSelect().then(response => {
                this.gatewayList = []
                for (let item of response.data) {
                    let temp = {}
                    temp['value'] = item.gatewayId
                    temp['label'] = item.gatewayName
                    temp['serverOnline'] = item.serverOnline
                    this.gatewayList.push(temp)
                }
            })
        },
        submitScreenSensor() {
            let length = this.screenSensorList.length
            for (let i = 0; i < length; i++) {
                this.screenSensorList[i]['orderNum'] = i + 1
            }
            // if (arr.length === 0) {
            //     this.$message.error('请选择信息点')
            //     return
            // }
            addScreenSensor({ screenId: this.form.id, dataList: this.screenSensorList }).then(response => {
                this.$message.success('添加成功')
                // this.screenSensorOpen = false
            })
        },
        cancelScreenSensor() {
            this.screenSensorOpen = false
        }
    }
}
</script>
